package com.example.common.utils;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author username
 * @version 1.0
 * @description: TODO
 * @date 2022/9/16 11:27
 */
@Component
@Slf4j
public class MyMetaObjecthandler implements MetaObjectHandler {
    /*
    * mybatis-plus内的自动填充的方法，可查看官方文档进行书写
    * */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段字段填充[insertFill]");
        log.info(metaObject.toString());
        // 判断是否存在 `isDelete` 字段
        if (metaObject.hasSetter("isdelete")) {
            metaObject.setValue("isdelete", 0);
        }

        // 判断是否存在 `createTime` 字段
        if (metaObject.hasSetter("createTime")) {
            metaObject.setValue("createTime", LocalDateTime.now());
        }

        // 判断是否存在 `updateTime` 字段
        if (metaObject.hasSetter("updateTime")) {
            metaObject.setValue("updateTime", LocalDateTime.now());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段字段填充[updateFill]");
        log.info(metaObject.toString());
        // 判断是否存在 `updateTime` 字段
        if (metaObject.hasSetter("updateTime")) {
            metaObject.setValue("updateTime", LocalDateTime.now());
        }
    }



}
